package com.mb.app.appused.base;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public abstract class SuperAdapter<T> extends BaseAdapter {

	List<T> list = new ArrayList<T>();
	Context context;
	LayoutInflater inflater;
	int layoutCout = -1;
	List<Integer> layoutType = new ArrayList<Integer>();
	List<Integer> listLayout = new ArrayList<Integer>();
	Boolean isSubmit=false;
	public abstract int getLayout();

	public abstract void setChildView(int position, T data, ViewHolder holder);

	public SuperAdapter(Context context) {
		// TODO Auto-generated constructor stub
		this.context = context;
		inflater = LayoutInflater.from(this.context);
	}

	public SuperAdapter(Context context, List<T> list) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.list = list;
		inflater = LayoutInflater.from(this.context);
		readType();
	}

	public void setList(List<T> list) {
		this.list = list;
		readType();
	}
	
	public void addData(T data){
		list.add(data);
	}
	public void submit(){
		readType();
	}


	void readType() {
		layoutType.clear();
		listLayout.clear();
		for (T t : this.list) {
			int layout = getLayout(t);
			listLayout.add(layout);
			layoutType.add(layout);
		}
		HashSet<Integer> h = new HashSet<Integer>(layoutType);
		layoutCout = h.size();

		for (int b = 0; b < layoutType.size(); b++) {
			int type = getType(layoutType.get(b), h);
			layoutType.set(b, type);
		}
		isSubmit=true;
	}

	@Override
	public void notifyDataSetChanged() {
		// TODO Auto-generated method stub
		readType();
		super.notifyDataSetChanged();
	}

	int getType(int layout, HashSet<Integer> h) {
		int sp = 0;
		for (int tp : h) {
			if (tp == layout)
				return sp;
			sp++;
		}
		return sp;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	public T getObject(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		if (layoutType == null || layoutType.size() < 2)
			return super.getItemViewType(position);
		return layoutType.get(position);
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		if (layoutCout < 1)
			return super.getViewTypeCount();
		return layoutCout;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder;
		if (convertView == null) {
			int layout = listLayout.get(position);
			convertView = inflater.inflate(layout, parent, false);
			viewHolder = new ViewHolder();
			viewHolder.view = convertView;
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		setChildView(position, list.get(position), viewHolder);
		return viewHolder.view;
	}

	@SuppressLint("DefaultLocale")
	int getLayout(T bean) {
		int a = getLayout();
		Method[] methods = bean.getClass().getMethods();
		for (int i = 0; i < methods.length; i++) {
			Method method = methods[i];
			if (method.getName().toLowerCase().compareTo("getlayout") == 0) {
				try {
					a = (Integer) method.invoke(bean);
					return a;
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return a;
	}

	public static class ViewHolder {
		public View view;

		public Object getObject(int id) {
			return view.findViewById(id);
		}

		public TextView getTextView(int id) {
			return (TextView) view.findViewById(id);
		}

		public Button getButtonView(int id) {
			return (Button) view.findViewById(id);
		}

		public Object getChildView(int id) {
			return view.findViewById(id);
		}
	}	
}
